<template>
  <div class="user-avator-dropdown">
    <Dropdown @on-click="handleClick">
    <a href="javascript:void(0)">
      <Avatar :src="userAvator"/>
      <span class="user-name">{{username}}</span>
    </a>
      <Icon :size="18" type="md-arrow-dropdown"></Icon>
      <DropdownMenu slot="list">
        <DropdownItem name="message">
          消息中心<Badge style="margin-left: 10px" :count="messageUnreadCount"></Badge>
        </DropdownItem>
        <DropdownItem name="editPassword">
         修改密码
        </DropdownItem>
        <DropdownItem name="logout">退出登录</DropdownItem>
      </DropdownMenu>
    </Dropdown>
  </div>
</template>

<script>
import './user.less'
import { mapActions } from 'vuex'
export default {
  name: 'User',
  props: {
    userAvator: {
      type: String,
      default: ''
    },
    messageUnreadCount: {
      type: Number,
      default: 0
    }
  },
  methods: {
    ...mapActions([
      'handleLogOut'
    ]),
    logout () {
      this.handleLogOut().then((res) => {
        if(res.code===200){
           this.$Message.success(res.msg);
           this.$router.push({
            name: 'login'
           })
        }
      })
    },
    message () {
      this.$router.push({
        name: 'message_page'
      })
    },
    handleClick (name) {
      switch (name) {
        case 'logout':
          this.logout()
          break
        case 'message':
          this.message()
          break
        case 'editPassword':
          this.$emit('edit-passwd')
          break;
      }
    }
  },
  computed:{
    username:function (){
      return this.$store.state.user.userName;
    }
  },
}
</script>
<style lang="less">
.user-name{
  font-size: 14px;
  margin-left: 5px;
}
</style>
